﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace IngredSubTool
{
    class PageSwitcher
    {
        public static MainWindow mainWindow;

        public static void Switch(UserControl newPage)
        {
            mainWindow.GoToPage(newPage);
        }
        public static void SwitchForward(UserControl newPage)
        {
                if (MainWindow.session.currentPage != newPage.GetType())
                {
                    MainWindow.session.lastPage = MainWindow.session.currentPage;
                    MainWindow.session.currentPage = newPage.GetType();
                    if (MainWindow.session.previousPagesList == null)
                    {
                        MainWindow.session.previousPagesList.AddFirst(MainWindow.session.lastPage);
                    }
                    else
                    {
                        MainWindow.session.previousPagesList.AddLast(MainWindow.session.lastPage);
                    }

                 
                }
                mainWindow.GoToPage(newPage);
        }

        public static void SwitchBack()
        {
            if (MainWindow.session.previousPagesList != null)
            {
                MainWindow.session.currentPage = MainWindow.session.previousPagesList.Last.Value;
                MainWindow.session.previousPagesList.RemoveLast();
                if (MainWindow.session.previousPagesList != null)
                {
                    MainWindow.session.lastPage = MainWindow.session.previousPagesList.Last.Value;
                }
                else
                {
                    MainWindow.session.lastPage = typeof(SearchView);
                }
                mainWindow.GoToPage((UserControl)Activator.CreateInstance(MainWindow.session.currentPage));
            }
            else
            {
                MainWindow.session.currentPage = typeof(SearchView);
                SwitchForward(new SearchView());
            }
        }
    }
}
